package com.dbgs.qqui;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.popup.QMUIListPopup;
import com.qmuiteam.qmui.widget.popup.QMUIPopup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public class PopUp {
    public static void init(final Activity activity){
        Button btn1 = activity.findViewById(R.id.btn1);
        Button btn2 = activity.findViewById(R.id.btn2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                QMUIPopup mNormalPopup =null;

                if (mNormalPopup == null) {
                    mNormalPopup = new QMUIPopup(activity, QMUIPopup.DIRECTION_NONE);

                    TextView textView = new TextView(activity);
                    textView.setLayoutParams(mNormalPopup.generateLayoutParam(
                            QMUIDisplayHelper.dp2px(activity, 250),
                            WRAP_CONTENT
                    ));
                    textView.setLineSpacing(QMUIDisplayHelper.dp2px(activity, 4), 1.0f);
                    int padding = QMUIDisplayHelper.dp2px(activity, 20);
                    textView.setPadding(padding, padding, padding, padding);
                    textView.setText("Popup 可以设置其位置以及显示和隐藏的动画");
                    textView.setTextColor(ContextCompat.getColor(activity, R.color.app_color_description));
                    mNormalPopup.setContentView(textView);
                    mNormalPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
//                            btn1.setText(activity.getResources().getString(R.string.popup_normal_action_button_text_show));
                        }
                    });

                }


                mNormalPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_LEFT);
                mNormalPopup.setPreferredDirection(QMUIPopup.DIRECTION_TOP);
                mNormalPopup.show(view);
//                    mActionButton1.setText(activity.getResources().getString(R.string.popup_normal_action_button_text_hide));

            }
        });



        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                QMUIListPopup mListPopup = null;

                if (mListPopup == null) {

                    String[] listItems = new String[]{
                            "Item 1",
                            "Item 2",
                            "Item 3",
                            "Item 4",
                            "Item 5",
                    };
                    List<String> data = new ArrayList<>();

                    Collections.addAll(data, listItems);

                    ArrayAdapter adapter = new ArrayAdapter<>(activity, R.layout.simple_list_item, data);

                    mListPopup = new QMUIListPopup(activity, QMUIPopup.DIRECTION_NONE, adapter);
                    mListPopup.create(QMUIDisplayHelper.dp2px(activity, 250), QMUIDisplayHelper.dp2px(activity, 200), new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            Toast.makeText(activity, "Item " + (i + 1), Toast.LENGTH_SHORT).show();
//                            mListPopup.dismiss();
                        }
                    });
                    mListPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
//                            mActionButton2.setText(activity.getResources().getString(R.string.popup_list_action_button_text_show));
                        }
                    });
                }


                mListPopup.setAnimStyle(QMUIPopup.ANIM_GROW_FROM_CENTER);
                mListPopup.setPreferredDirection(QMUIPopup.DIRECTION_TOP);
                mListPopup.show(view);
//                mActionButton2.setText(getContext().getResources().getString(R.string.popup_list_action_button_text_hide));

            }
        });
    }
}
